package com.gpframework.module.wages.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.wages.domain.GzTotalSalesItems;
import com.gpframework.module.wages.domain.GzTotalSalesOfTheCompany;
import com.gpframework.module.wages.domain.GzWarehouseEvaluation;
import com.gpframework.module.wages.domain.req.GzTotalSalesOfTheCompanyAdd;
import com.gpframework.module.wages.domain.vo.GzTotalSalesItemsVo;
import com.gpframework.module.wages.domain.vo.GzTotalSalesOfTheCompanyVo;
import com.gpframework.module.wages.mapper.GzTotalSalesOfTheCompanyMapper;
import com.gpframework.module.wages.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
/**
 * @ClassName GzTotalSalesOfTheCompany
 * @Author gpframework
 * @Date 2024-01-19
 * @Version 1.0.0
 * @Description 公司总销售Service业务层处理
 */
@Service
public class GzTotalSalesOfTheCompanyServiceImpl extends ServiceImpl<GzTotalSalesOfTheCompanyMapper, GzTotalSalesOfTheCompany> implements IGzTotalSalesOfTheCompanyService {
    @Autowired
    private IGzAttendanceManagementService iGzAttendanceManagementService;
    //人员基础
    @Autowired
    private IGzPersonnelFoundationService iGzPersonnelFoundationService;
    @Autowired
    private IGzWarehouseEvaluationService iGzWarehouseEvaluationService;

    @Autowired
    private IGzTotalSalesItemsService iGzTotalSalesItemsService;




    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-19
     * @Param [lqw]
     * @Return List<GzTotalSalesOfTheCompanyVo>
     */
    @Override
    public List<GzTotalSalesOfTheCompanyVo> listVo(QueryWrapper<GzTotalSalesOfTheCompany> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-19
     * @Param [id]
     * @Return GzTotalSalesOfTheCompanyVo
     */
    @Override
    public GzTotalSalesOfTheCompanyVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public boolean addTotalSalesOfTheCompany(GzTotalSalesOfTheCompanyAdd gzTotalSalesOfTheCompanyAdd) {
        this.deleteByMonth(gzTotalSalesOfTheCompanyAdd.getMonth());
        GzTotalSalesOfTheCompany gzTotalSalesOfTheCompany = Convert.convert(new TypeReference<GzTotalSalesOfTheCompany>() {}, gzTotalSalesOfTheCompanyAdd);
        //动态计算公司总销售条数
        double totalSalesItems = calculatePrice(gzTotalSalesOfTheCompanyAdd.getTotalSales());
        gzTotalSalesOfTheCompany.setTotalSalesItems(BigDecimal.valueOf(totalSalesItems).add(gzTotalSalesOfTheCompanyAdd.getTotalSalesKgPieces()));

        //仓库系数
        double totalPerformanceCoefficient=0;
        List<GzWarehouseEvaluation> warehouseEvaluations = iGzWarehouseEvaluationService.list(new QueryWrapper<GzWarehouseEvaluation>().eq("month", gzTotalSalesOfTheCompanyAdd.getMonth()));
        for (GzWarehouseEvaluation warehouseEvaluation : warehouseEvaluations) {
            /** 当月按天数绩效 */
            totalPerformanceCoefficient+=warehouseEvaluation.getMonthlyPerformanceByDays();
        }

        gzTotalSalesOfTheCompany.setWarehouseCoefficient(totalPerformanceCoefficient);

        //总绩效系数 =仓库系数+拿货系统
        gzTotalSalesOfTheCompany.setTotalPerformanceCoefficient(gzTotalSalesOfTheCompany.getWarehouseCoefficient() +gzTotalSalesOfTheCompany.getPickupSystem());

        //  月平均绩效金额=总销售条数 /总绩效系数
        gzTotalSalesOfTheCompany.setMonthlyAveragePerformanceAmount(gzTotalSalesOfTheCompany.getTotalSalesItems().divide(BigDecimal.valueOf(gzTotalSalesOfTheCompany.getTotalPerformanceCoefficient()),2, RoundingMode.HALF_UP));

        gzTotalSalesOfTheCompany.setMonth(gzTotalSalesOfTheCompanyAdd.getMonth());
        this.save(gzTotalSalesOfTheCompany);
        return true;
    }

    private void deleteByMonth(Date month) {
        this.baseMapper.delelteMonth(month);
    }

    public double calculatePrice(BigDecimal total) {
        List<GzTotalSalesItemsVo> list = iGzTotalSalesItemsService.listVo(new QueryWrapper<GzTotalSalesItems>().orderByDesc("number"));
        BigDecimal price = new BigDecimal(0);

        GzTotalSalesItemsVo gzTotalSalesItemsfirst = list.get(0);
        String[] itemsOne = StringUtils.split(gzTotalSalesItemsfirst.getNumberOfItems(), "-");

        for (int i = 0; i < itemsOne.length; i++) {
            int i1 = total.compareTo(new BigDecimal(itemsOne[0]));
            int i2 = total.compareTo(new BigDecimal(itemsOne[1]));
            if (i1>=0 && i2<0){
                price = total.multiply(gzTotalSalesItemsfirst.getNumber());
            }
        }

        GzTotalSalesItemsVo gzTotalSalesItemsSecend = list.get(1);
        String[] itemstwo = StringUtils.split(gzTotalSalesItemsSecend.getNumberOfItems(), "-");
        for (int i = 0; i < itemstwo.length; i++) {
            int i1 = total.compareTo(new BigDecimal(itemstwo[0]));
            int i2 = total.compareTo(new BigDecimal(itemstwo[1]));
            if (i1>=0 && i2<0){
                BigDecimal priceOne = (new BigDecimal(itemsOne[1])).multiply(gzTotalSalesItemsfirst.getNumber());
                BigDecimal priceTwo = total.subtract(new BigDecimal(itemstwo[0])).multiply(gzTotalSalesItemsSecend.getNumber());
                price = priceOne.add(priceTwo);
            }
        }

        GzTotalSalesItemsVo gzTotalSalesItemsthird = list.get(2);
        String[] itemsthree = StringUtils.split(gzTotalSalesItemsthird.getNumberOfItems(), "-");
        for (int i = 0; i < itemsthree.length; i++) {
            int i1 = total.compareTo(new BigDecimal(itemsthree[0]));
            if (i1>=0){
                BigDecimal priceOne = (new BigDecimal(itemsOne[1])).multiply(gzTotalSalesItemsfirst.getNumber());
                BigDecimal priceTwo = (total.subtract(new BigDecimal(itemsOne[1])).divide(BigDecimal.valueOf(10000),0, RoundingMode.DOWN)).multiply(gzTotalSalesItemsSecend.getNumber()).multiply(BigDecimal.valueOf(10000));
                BigDecimal priceThreed = (total.remainder(BigDecimal.valueOf(10000))).multiply(gzTotalSalesItemsthird.getNumber());
                price = priceOne.add(priceTwo).add(priceThreed);
            }
        }

        // 计算公式
        //if (total<20000 && total>0){
        //    price = total * 1.4;
        //} else if(total>20000 && total<30000) {
        //    price = 20000 * 1.4 + ((total-20000)*1.3);
        //} else if (total>30000){
        //    price =20000*1.4 + (((total-20000)/10000)*1.3*10000)+((total%10000)*1.2);
        //}

        return price.doubleValue();
    }

}
